$(function() {
    $("#pendingTable").bootstrapTable('destroy').bootstrapTable({
        url : window.portAddress + "/pending/pendingList",
        dataType : "json",
        method : 'get',
        striped: true,  //行间隔色
        cache : false,  //禁用缓存
        pageNumber: 1,  //初始化加载第一页
        pagination: true,//是否分页
        sidePagination: "client",//前端分页
        pageSize: 5,//单页记录数
        pageList: [5, 10, 15],//可选择单页记录数
        clickToSelect : true,
        theadClasses: "thead-pink",
        queryParams: function (params) {
            //向服务器传递的参数
            return {
               /* option : $("#search_option option:selected").val(),*/
                status : $("#status").val(),
                txType : $("#txType").val(),
            };
        },
        responseHandler : function(res) {
            return res.data;
        },
        columns : [
            // {
            //     title : "",
            //     radio: true,
            // },
            {
                title : "审批编号",
                field : "pendingNum",
                sortable : true,
                align : "center",
            },
            {
                title : "操作类型",
                field : "txType",
                align : "center",

            },
            {
                title : "请求内容",
                field : "requestType",
                align : "center",
                width : 700,

            },
            {
                title : "状态",
                field : "status",
                align : "center",
            },
            {
                title : "申请人",
                field : "maker",
                align : "center",
            },
            {
                title : "审批人",
                field : "approver",
                align : "center",
            },
            {
                title : "请求时间",
                field : "requestDateTime",
                align : "center",
            },
            {
                title : "备注",
                field : "remark",
                align : "center",
            },
            {
                title : "操作",
                field : "status",
                formatter : operation,
                width : 130,
                height: 40,
                align : "center",
            }

        ]
    });

    function operation(value,row,index) {
        var  htm;
        if(value == 'APP'){
            return htm = "<span class=\"badge badge-warning\">APPROVED</span>"
        }else if (value == 'REJ'){
            return htm = "<span class=\"badge badge-warning\">REJECTED</span>"
        }else {
                htm = "<button type=\"button\" class=\"btn btn-primary\" style='width: 40px;' data-target=\"#approveModel\" data-toggle=\"modal\"><span class=\"glyphicon glyphicon-ok\"></span></button> "
                    + "<button type=\"button\" class=\"btn btn-danger\" style='width: 40px;' data-target=\"#rejectModel\" data-toggle=\"modal\"><span class=\"glyphicon glyphicon-remove\"></span></button> ";
        }
        return htm;
    }

    $("#rejectModel").on("show.bs.modal",function (event){
        var thisBtn = $(event.relatedTarget);
        var pendingNumber = thisBtn.closest('tr').find('td').eq(0).text();
        $("#pendigNumber").val(pendingNumber);
    });

    $("#rejectPending").click(function() {
        $.ajax({
            url : window.portAddress + "/pending/rejectPending",
            type : "post",
            data : {
                pendingNum : $("#pendigNumber").val(),
                userName: window.sessionStorage.getItem("userName"),
                remark:$("#remark").val()
            },
            dataType : "json",
            success : function(data) {
                $("#pendingTable").bootstrapTable("refresh");
                $("#rejectModel").modal('hide');

                if (data.state === '000000') {
                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
            }
        })
    });

    $("#approveModel").on("show.bs.modal",function (event){
        var thisBtn = $(event.relatedTarget);
        var pendingNumber = thisBtn.closest('tr').find('td').eq(0).text();
        $("#approvePendingNumber").val(pendingNumber);
    });

    $("#approvePending").click(function() {
        $.ajax({
            url : window.portAddress + "/pending/approvePending",
            type : "post",
            data : {
                pendingNum : $("#approvePendingNumber").val(),
                userName: window.sessionStorage.getItem("userName"),
                remark:$("#approveRemark").val()
            },
            dataType : "json",
            success : function(data) {
                $("#pendingTable").bootstrapTable("refresh");
                $("#approveModel").modal('hide');

                if (data.state === '000000') {
                    toastr.success(data.message);
                }else {
                    toastr.error(data.message);
                }
            }
        })
    });

    $("#search").click(function() {
        $.ajax({
            url : window.portAddress + "/pending/pendingList",
            type : "post",
            data : {
                status : $("#status").val(),
                txType : $("#txType").val(),
            },
            dataType : "json",
            success : function(data) {
                $("#pendingTable").bootstrapTable("refresh");
                $("#rejectModel").modal('hide');
                if (data.state === '000000') {
                    toastr.success("查询数据成功");
                }else {
                    toastr.error(data.message);
                }
            }
        })
    });


})
